package com.shengqi.xinnongren.work.ui.goods.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.utils.ImageUtils;
import com.shengqi.xinnongren.work.ui.home.model.CartGoodsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderGoodsAdapter extends RecyclerView.Adapter<OrderGoodsAdapter.Holder> {

    private Context mContext;
    private List<CartGoodsBean> mCartGoodsList;

    public OrderGoodsAdapter(Context context, List<CartGoodsBean> cartGoodsList) {
        mContext = context;
        mCartGoodsList = cartGoodsList;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(mContext).inflate(R.layout.item_order_goods, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        CartGoodsBean cartGoodsBean = mCartGoodsList.get(position);
        holder.tvTitle.setText(cartGoodsBean.getComName());
        ImageUtils.showImage(mContext, cartGoodsBean.getPicUrl(), holder.ivCover);
        holder.setDataList(cartGoodsBean.getSkuList());
    }

    @Override
    public int getItemCount() {
        return mCartGoodsList.size();
    }

    public class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.rv_order_goods_params)
        RecyclerView mRvParams;
        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        private ParamsAdapter mAdapter;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            init();
        }

        List<CartGoodsBean.SkuMapBean> mSkuBeanList = new ArrayList<>();

        public void setDataList(List<CartGoodsBean.SkuMapBean> skuBeanList) {
            if (skuBeanList == null || skuBeanList.isEmpty()) return;
            mSkuBeanList.clear();
            mSkuBeanList.addAll(skuBeanList);
            mAdapter.notifyDataSetChanged();
        }

        private void init() {
            mAdapter = new ParamsAdapter(mSkuBeanList);
            mRvParams.setAdapter(mAdapter);
            mRvParams.setLayoutManager(new LinearLayoutManager(mContext) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
        }


    }

    class ParamsAdapter extends RecyclerView.Adapter<ParamsAdapter.Holder> {
        List<CartGoodsBean.SkuMapBean> mSkuBeanList;

        public ParamsAdapter(List<CartGoodsBean.SkuMapBean> skuBeanList) {
            mSkuBeanList = skuBeanList;
        }

        @NonNull
        @Override
        public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new Holder(LayoutInflater.from(mContext).inflate(R.layout.item_order_goods_params, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull Holder holder, int position) {
            CartGoodsBean.SkuMapBean skuMapBean = mSkuBeanList.get(position);
            holder.tvSpecification.setText(String.format("￥%s", skuMapBean.getPrice()));
            holder.tvNum.setText(String.format("x%s", skuMapBean.getShopCarNum()));
            holder.tvUnitPrice.setText(skuMapBean.getSkuName());
        }

        @Override
        public int getItemCount() {
            return mSkuBeanList.size();
        }

        public class Holder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_specification)
            TextView tvSpecification;
            @BindView(R.id.tv_num)
            TextView tvNum;
            @BindView(R.id.tv_unit_price)
            TextView tvUnitPrice;

            public Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}